package com.example.demo.java8.stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 简单的Stream
 *
 * @author Roger
 * @since 2021/7/25
 */
public class SimpleStreamTest {

    public static void main(String[] args) {

        List<String> list = new ArrayList();
        Arrays.asList("1","2","3");
        list.add("1");
        list.add("2");
        list.add("3");
        //循环
        list.stream().forEach(string ->{
            System.out.println(string);
        });
        List<String> list2 = new ArrayList();
        //向另一个list赋值
        list2 = list.stream().map(string -> {
            return "stream().map()处理之后：" + string;
        }).collect(Collectors.toList());
        //转set
        Set<String> publicTargetIdSet = list.stream().map(string -> {
            return "stream().map()处理之后：" + string;
        }).collect(Collectors.toSet());
        //利用stream().filter()处理List,对该List进行过滤处理
        list2 = list.stream().filter(s -> s != "1").collect(Collectors.toList());
        System.out.println(list2.toString());
        
        //求和
        int sum = list.stream().mapToInt(v->Integer.valueOf(v)).sum();
        System.out.println("求和:"+sum);
    }
}
